var curD = new Date();
var firstD = new Date();
var lastD = new Date();
var dayTime = 24 * 60 * 60 * 1000;
var startYear;
var startMonth;


function calculateDate() {
	curD.setHours(12, 12, 12, 0);
	var dateLong = curD.getTime();

	if (curD.getDay() == 0) {
		dateLong = dateLong - (6 * dayTime);
	} else {
		dateLong = dateLong - (curD.getDay() - 1) * dayTime;
	}

	curD.setTime(dateLong);

	firstD = curD;
	lastD.setTime(firstD.getTime() + 6 * dayTime);
	
	var strMon = firstD.getMonth()+1;
	strMon = (strMon < 10 ? "0"+strMon: strMon);
	var strDay = firstD.getDate();
	strDay = (strDay < 10 ? "0"+strDay : strDay);
	var st = firstD.getFullYear()+'-'+ strMon +'-'+strDay;
	
	strMon = lastD.getMonth()+1;
	strMon = (strMon < 10 ? "0"+strMon: strMon);
	strDay = lastD.getDate();
	strDay = (strDay < 10 ? "0"+strDay : strDay);
	var sd = lastD.getFullYear()+'-'+ strMon +'-'+strDay;
	
	var ss = st +' 至 '+ sd;
	
	document.getElementById('dateScope').setAttribute('value', ss);
	
	var noweek = getWeekNumber(st);
	document.getElementById('week').setAttribute('value',"第"+noweek+"周");
}

function backDate() {
	var dateLong = curD.getTime();
	curD.setTime(dateLong - (6 * dayTime));
	calculateDate();
}

function nextDate() {
	var dateLong = curD.getTime();
	curD.setTime(dateLong + (7 * dayTime));
	calculateDate();
}

function getWeekNumber(d) {
	// Copy date so don't modify original
	d = new Date(d);
	d.setHours(0, 0, 0);
	// Set to nearest Thursday: current date + 4 - current day number
	// Make Sunday's day number 7
	d.setDate(d.getDate() + 4 - (d.getDay() || 7));
	// Get first day of year
	var yearStart = new Date(startYear, startMonth, 1);
	// Calculate full weeks to nearest Thursday
	var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
	// Return array of year and week number
	return weekNo;
}